package com.weixin.paleituo.fragment;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.AppoinmentRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.LazyBaseFragment;
import com.weixin.paleituo.bean.AppoinmentBean;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LoadingDialogUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;
import com.weixin.paleituo.view.YRecycleview;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * Author yichao
 * Time  2018/4/7 17:14
 * Dest 已预约
 */
public class AreadAppoinmentFragment extends LazyBaseFragment{
    @BindView(R.id.yrecycleview_fragment_awaitpayfor)
    YRecycleview mYrecycleviewFragmentAwaitpayfor;
    private LoadingPagerHead mLoadingPagerHead;
    private List<AppoinmentBean> mStoreListBeans=new ArrayList<>();
    private AppoinmentRecycleAdapter mAppoinmentRecycleAdapter;
    private int curentPag=1;
    private int orderStatus=1;
    private boolean mIsPrepared;
    private Dialog mLoadingDialog;
    private boolean isFirst=true;
    @Override
   // AreadVisterAppoinmentFragment
   // CancelAppoinmentFragment
    public View initView(Context context) {

        if (mLoadingPagerHead==null){
        mLoadingPagerHead = new LoadingPagerHead(mContext,R.layout.appionment_empty_layout) {
            @Override
            protected void close() {

            }
            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(mContext, R.layout.fragment_awaitpayfor);
            }
            @Override
            protected void reLoading() {
                retry();
            }
        };
          }
        return mLoadingPagerHead;
    }

    /**
     * 重试
     */
    private void retry() {
        curentPag=1;

        Observable<StatusCode<List<AppoinmentBean>>> orderListObservable = ApiUtils.getApi().getAppoinmentList(BaseApplication.sUserInfoBean.getToken(), BaseApplication.sUserInfoBean.userId, orderStatus, 1, Contans.rows);
        HttpUtil.getInstance().toSubscribe(orderListObservable, new ProgressSubscriber<List<AppoinmentBean>>(mContext) {
            @Override
            protected void _onNext(StatusCode<List<AppoinmentBean>> listStatusCode) {
                LoadingDialogUtils.closeDialog(mLoadingDialog);
                List<AppoinmentBean> appoinmentBeansList = listStatusCode.getData();
                if (appoinmentBeansList!=null&&appoinmentBeansList.size()>0){
                    mStoreListBeans.clear();
                    mStoreListBeans.addAll(appoinmentBeansList);
                    mAppoinmentRecycleAdapter.notifyDataSetChanged();
                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);

                }else {
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                LoadingDialogUtils.closeDialog(mLoadingDialog);
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);
                ToastUtils.makeText(message);
            }
        },"",lifecycleSubject,false,true);
    }
    @Override

    protected void lazyLoad() {
        if(!mIsPrepared || !isVisible) {
            return;
        }
        load();
    }

    /**
     * 懒加载数据
     */
    private void load() {

        if (mLoadingDialog==null){
            mLoadingDialog = LoadingDialogUtils.createLoadingDialog(getActivity(), "");
        }
        LoadingDialogUtils.show(mLoadingDialog);
        retry();

    }



    @Override
    protected void initData() {
        super.initData();

        mIsPrepared = true;
        mStoreListBeans.clear();
        mAppoinmentRecycleAdapter = new AppoinmentRecycleAdapter(mContext,mStoreListBeans);
        mYrecycleviewFragmentAwaitpayfor.setLayoutManager(new LinearLayoutManager(mContext));
        mYrecycleviewFragmentAwaitpayfor.setItemAnimator(new DefaultItemAnimator());
        mYrecycleviewFragmentAwaitpayfor.setAdapter(mAppoinmentRecycleAdapter);

        mYrecycleviewFragmentAwaitpayfor.setRefreshAndLoadMoreListener(new YRecycleview.OnRefreshAndLoadMoreListener() {
            @Override
            public void onRefresh() {
                Observable<StatusCode<List<AppoinmentBean>>> orderListObservable = ApiUtils.getApi().getAppoinmentList(BaseApplication.sUserInfoBean.getToken(), BaseApplication.sUserInfoBean.userId, orderStatus, 1, Contans.rows);
                HttpUtil.getInstance().toSubscribe(orderListObservable, new ProgressSubscriber<List<AppoinmentBean>>(mContext) {
                    @Override
                    protected void _onNext(StatusCode<List<AppoinmentBean>> listStatusCode) {
                        List<AppoinmentBean> goodsOrderBeans = listStatusCode.getData();
                        mYrecycleviewFragmentAwaitpayfor.setReFreshComplete();
                        if (goodsOrderBeans!=null&&goodsOrderBeans.size()>0){
                            mStoreListBeans.clear();
                            mStoreListBeans.addAll(goodsOrderBeans);
                            curentPag=1;
                            mAppoinmentRecycleAdapter.notifyDataSetChanged();

                        }else {
                            ToastUtils.makeText("刷新失败");
                        }

                    }

                    @Override
                    protected void _onError(String message) {
                        mYrecycleviewFragmentAwaitpayfor.setReFreshComplete();
                        ToastUtils.makeText(message);


                    }
                },"",lifecycleSubject,false,true);


            }

            @Override
            public void onLoadMore() {

                Observable<StatusCode<List<AppoinmentBean>>> orderListObservable = ApiUtils.getApi().getAppoinmentList(BaseApplication.sUserInfoBean.getToken(), BaseApplication.sUserInfoBean.userId, orderStatus, curentPag+1, Contans.rows);
                HttpUtil.getInstance().toSubscribe(orderListObservable, new ProgressSubscriber<List<AppoinmentBean>>(mContext) {
                    @Override
                    protected void _onNext(StatusCode<List<AppoinmentBean>> listStatusCode) {
                        List<AppoinmentBean> goodsOrderBeans = listStatusCode.getData();
                        mYrecycleviewFragmentAwaitpayfor.setloadMoreComplete();
                        if (goodsOrderBeans!=null&&goodsOrderBeans.size()>0){


                            mStoreListBeans.addAll(goodsOrderBeans);
                            mAppoinmentRecycleAdapter.notifyDataSetChanged();
                            curentPag+=1;

                        }else {
                            ToastUtils.makeText("没有更多数据了");
                        }
                    }
                    @Override
                    protected void _onError(String message) {
                        mYrecycleviewFragmentAwaitpayfor.setloadMoreComplete();
                        ToastUtils.makeText(message);
                    }
                },"",lifecycleSubject,false,true);


            }
        });

        retry();
    }


    @Override
    public void onResume() {
        super.onResume();

        if (isFirst){

            isFirst=false;

        }else {

if (isVisible){
            load();

}

        }


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        mLoadingDialog=null;
        mIsPrepared=false;
        isFirst= true;
    }

}
